<template>
  <view>
    <view class="search-box">
      <uni-search-bar @input="input" cancelButton="none" :radius="100" :focus="true" />
    </view>
    <uni-list v-if="kw.length !== 0" type="line">
      <uni-list-item class="list-item" @click.native="gotoGoods(item)" showArrow :title="item.goods_name"
        v-for="item in serchResuList" :key="item.goods_id" />
    </uni-list>
    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <!-- 标题区域 -->
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="clear"></uni-icons>
      </view>
      <!-- 列表区域 -->
      <view class="history-list">
        <uni-tag :text="item" v-for="(item, i) in historys" :key="i" @click="gotoGoodList(item)"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name: '',
    components: {},
    data() {
      return {
        kw: '', //输入的字符串
        timer: undefined, //防抖
        serchResuList: [], //获取到的数据
        historyList: [], //搜索历史关键词
      };
    },
    created() {},
    mounted() {},
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
    },
    computed: {
      historys() {
        // 注意：由于数组是引用类型，所以不要直接基于原数组调用 reverse 方法，以免修改原数组中元素的顺序
        // 而是应该新建一个内存无关的数组，再进行 reverse 反转

        return [...this.historyList].reverse()
      }
    },
    methods: {
      gotoGoods(item) {
        console.log(item)
        uni.navigateTo({
          url: '../goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      },
      input(e) {
        if (this.timer) {
          clearTimeout(this.timer)
          this.timer = undefined
        }
        this.timer = setTimeout(() => {
          this.kw = e
          this.getSearchResults()
        }, 500)
      },
      async getSearchResults() {
        if (this.kw.length == 0) {
          return this.serchResuList = []
        }
        let {
          data: {
            meta: {
              status
            },
            message
          }
        } = await uni.$http.get('/api/public/v1/goods/qsearch', {
          query: this.kw
        });
        if (status !== 200) uni.$showMsg()
        this.serchResuList = message
        this.sethistory()
        console.log(this.serchResuList)
      },
      sethistory() {
        // 直接把搜索关键词 push 到 historyList 数组中
        this.historyList.push(this.kw)
        const arr = new Set(this.historyList)
        arr.delete(this.kw)
        arr.add(this.kw)
        this.historyList = [...arr]
        uni.setStorageSync('kw', JSON.stringify(this.historyList))
      },
      clear() {
        this.historyList = []
        uni.setStorageSync('kw', JSON.stringify(this.historyList))
      },
      gotoGoodList(item) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?query=' + item
        })
      }
    }

  }
</script>

<style lang="scss">
  .uni-searchbar {
    background-color: #c00000;
  }

  .search-box {
    position: sticky;
    top: 0;
    z-index: 99999;
  }

  .list-item {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .history-box {
    padding: 0 5px;

    .history-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 40px;
      font-size: 13px;
      border-bottom: 1px solid #efefef;
    }

    .history-list {
      display: flex;
      flex-wrap: wrap;

      .uni-tag {
        margin-top: 5px;
        margin-right: 5px;
      }
    }
  }
</style>
